package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.Title;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.TitleService;
import com.wty.cmims.mapper.TitleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【title】的数据库操作Service实现
* @createDate 2024-11-19 21:50:22
*/
@Service
public class TitleServiceImpl extends ServiceImpl<TitleMapper, Title>
    implements TitleService{
    @Autowired
    private TitleMapper titleMapper;
    @Override
    public ResponseResult<Map<String, Object>> titleFind(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> titles = titleMapper.findTitle(paramsMap);
        if (titles != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询职称信息成功");
            responseResult.setDatas(titles);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询职称信息失败");
        }
        return responseResult;
    }

    @Override
    @Transactional
    public ResponseResult<Map<String, Object>> titlesFind(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);

        List<Map<String, Object>> titles = titleMapper.findTitles(paramsMap);
        Integer total = titleMapper.findTitlesNum(paramsMap);
        if (titles != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询科室信息成功");
            responseResult.setDatas(titles);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询科室信息失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> titleUpdate(Title title) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        boolean result = titleMapper.updateById(title) > 0;
        if (result) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("修改科室信息成功");
        } else {
            responseResult.setCode(Code.ERROR);
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> titleAdd(Title title) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        int rows = titleMapper.insert(title);
        if (rows > 0) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("添加科室信息成功");
        } else {
            responseResult.setCode(Code.ERROR);
        }
        return responseResult;
    }
}




